<?php

class TieuChi {
	private static $prefix = array("SV_", "CVHT_", "HDK_");
	private static $SV_ = true;
	private static $CVHT_ = true;
	private static $HDK_ = true;
	
	public static function setEnable($prefix) {
		self::$SV_ = self::$CVHT_ = self::$HDK_ = true;
		self::${$prefix} = false;
	}
	
	public static function formatRow($indent, $maTieuChi, $maSinhVien, $maHocKy, $maMucGoc, $thuTuMuc, &$dbConn) {		
		$dbConn->setQuery("SELECT * FROM TIEU_CHI WHERE TC_MA_SO = " . $maTieuChi);
		$tieuChi = mysql_fetch_assoc($dbConn->fetchAll());
		$indent = ($indent == 1 ? "- " : "&nbsp;&nbsp;&nbsp;+ ");
		
		//$DEBUG = array();
		
		echo '<tr>';
		echo '<td>' . $indent . $tieuChi['TC_TEN'] . ' ' . $tieuChi['TC_DIEN_GIAI'] . '</td>';
		
		for ($i = 0; $i < count(self::$prefix); $i++) {
			$pre = self::$prefix[$i];
			$preNoDash = str_replace('_', '', $pre);
						
			echo '<td class="tieu_chi">';
			echo '<select name="' . $pre . 'Muc' . $maMucGoc . '_cboTC' . $maTieuChi . '" ';
			if (self::${$pre}) echo 'disabled="true" ';
			else echo 'onchange="capNhatTongDiemCuaMuc('.($thuTuMuc-1).');" ';
			
			$dbConn->setQuery('SELECT DIEM_' . $preNoDash . ' as DIEM FROM CHAM_DIEM_' . $preNoDash
							 . ' WHERE TC_MA_SO = ' . $maTieuChi . ' AND HK_MA_SO = ' . $maHocKy
							 . ' AND SV_MA_SO = ' . $maSinhVien);
			$diemCu = mysql_fetch_assoc($dbConn->fetchAll());
			//$DEBUG[$preNoDash] = $diemCu;
			if ($diemCu == false) {
				$diemCu = array('DIEM' => 0);
				
				if ($preNoDash == "CVHT") {
					$dbConn->setQuery('SELECT DIEM_SV as DIEM FROM CHAM_DIEM_SV'
									 . ' WHERE TC_MA_SO = ' . $maTieuChi . ' AND HK_MA_SO = ' . $maHocKy
									 . ' AND SV_MA_SO = ' . $maSinhVien);
					if ($diemSV = mysql_fetch_assoc($dbConn->fetchAll())) {
						$diemCu = $diemSV;
					}
				} else if ($preNoDash == "HDK") {
					$dbConn->setQuery('SELECT DIEM_CVHT as DIEM FROM CHAM_DIEM_CVHT'
									 . ' WHERE TC_MA_SO = ' . $maTieuChi . ' AND HK_MA_SO = ' . $maHocKy
									 . ' AND SV_MA_SO = ' . $maSinhVien);
					if ($diemCVHT = mysql_fetch_assoc($dbConn->fetchAll())) {
						$diemCu = $diemCVHT;
					}
				}
			}
			
			$options = "";
			$j = 0;
			for ($j = 0; $j < $diemCu['DIEM']; $j += $tieuChi['TC_BUOC_DIEM'])
				$options .= '<option value="' . $j . '">' . $j . '</option>';
			
			$options .= '<option value="' . $j . '" selected="selected">' . $j . '</option>';
			
			for ($j += $tieuChi['TC_BUOC_DIEM']; $j <= $tieuChi['TC_DIEM_TOI_DA']; $j += $tieuChi['TC_BUOC_DIEM'])
				$options .= '<option value="' . $j . '">' . $j . '</option>';
			
			echo '>' . $options . '</select></td>';
		}
		echo '</tr>';
		
		//var_dump($DEBUG);
	}
	
	public static function layDanhSachMaTieuChi(&$db) {
		$dsMaTieuChi = array();
		
		$result = $db->select("BANG_DIEM_REN_LUYEN", array("BDRL_MA_SO"),
	  						  "BDRL_NGAY_AP_DUNG = (select max(BDRL_NGAY_AP_DUNG) from BANG_DIEM_REN_LUYEN)");
		$row = mysql_fetch_assoc($result);
		$maBangDiem = $row['BDRL_MA_SO'];
		
		$db->setQuery("SELECT M.MUC_MA_SO, MUC_DIEM_TOI_DA FROM CO_MUC C, MUC M "
					 . "WHERE BDRL_MA_SO = $maBangDiem AND C.MUC_MA_SO = M.MUC_MA_SO");
		$result = $db->fetchAll();
		while ($row = mysql_fetch_assoc($result)) {
			$dsMaTieuChi[$row['MUC_MA_SO']]['DIEM_TOI_DA'] = intval($row['MUC_DIEM_TOI_DA']); 
			$dsMaTieuChi[$row['MUC_MA_SO']]['DS_TIEU_CHI'] = self::layTieuChiTheoMuc($row['MUC_MA_SO'], $db);
			//$dsMaTieuChi[] = self::layTieuChiTheoMuc($row['MUC_MA_SO'], $db);
		}
		return $dsMaTieuChi;
	}
	
	private static function layTieuChiTheoMuc($maMuc, &$db) {
		$data = array();
		$db->setQuery("(SELECT TC_MA_SO as MUC_CON, TC_CO_THU_TU as THU_TU, @KHOAN := 0 as LA_KHOAN "
					 . "FROM muc a, co_tieu_chi b WHERE a.muc_ma_so = " . $maMuc . " and a.muc_ma_so = b.muc_ma_so) "
					 . "UNION (SELECT KHOAN_MA_SO as MUC_CON, KHOAN_CO_THU_TU as THU_TU, @KHOAN := 1 as LA_KHOAN "
					 . "FROM muc a, co_khoan c WHERE a.muc_ma_so = " . $maMuc . " and a.muc_ma_so = c.muc_ma_so)");
		$result = $db->fetchAll();
		
		while ($row = mysql_fetch_assoc($result)) {
			if ($row['LA_KHOAN'] == 1)
				$data = array_merge($data, self::layTieuChiTheoMuc($row['MUC_CON'], $db));
				//$data[] = self::layTieuChiTheoMuc($row['MUC_CON'], $db);
			else $data[] = intval($row['MUC_CON']);
		}
		
		return $data;
	}
}

?>